﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Quan_ly_thong_tin_he_thong
{
    public partial class ThemTinTuc : Form
    {
        string filename;
        string tenanh;
        string taikhoan;

        public ThemTinTuc()
        {
            InitializeComponent();
        }

        public ThemTinTuc(string tk)
        {
            InitializeComponent();
            this.taikhoan = tk;
        }

        private void xoaThongTin()
        {
            txtTieuDe.Text = null;
            hinhAnhTinTuc.Image = null;
            txtNoiDung.Text = null;
            txtNoiDungChinh.Text = null;
        }

        private void chonAnhButton_Click(object sender, EventArgs e)
        {
            OpenFileDialog f = new OpenFileDialog();
            f.InitialDirectory = @"E:\Bai hoc\Lap trinh web 1\quan-ly-trang-suc-m3t\source\Quan ly thong tin he thong\Quan ly thong tin he thong\Anh nhan vien";
            f.Filter = "Images Files (*.bmp, *jpg, *.png)|*.bmp; *.jpg; *.png|All Files (*.*)|*.*";
            if (f.ShowDialog() == DialogResult.OK)
            {
                filename = f.FileName;
                tenanh = System.IO.Path.GetFileName(f.FileName);
                hinhAnhTinTuc.Image = Image.FromFile(filename);
            }
        }

        private void themTinTuc(string hinhAnh, string tieuDe, string noidungdemo, string noidung)
        {
            Config file = new Config(Application.StartupPath + "\\config.ini");
            string connectionstring = file.ReadValue("server", "con");
            SqlConnection conn = new SqlConnection(connectionstring);
            string sql = "insert into TinTuc(MaSo_TT,HinhAnhTieuDe,TieuDe,NoiDungDemo,NoiDung,NgayDang,MaSo_NV) values('',@HinhAnhTieuDe,@TieuDe,@NoiDungDemo,@NoiDung,getdate(),(select MaSo_NV from NhanVien where TenDangNhap_NV = @TenDangNhap_NV))";
            SqlCommand cmd = new SqlCommand(sql, conn);
            conn.Open();
            cmd.Parameters.AddWithValue("@HinhAnhTieuDe", hinhAnh);
            cmd.Parameters.AddWithValue("@TieuDe", tieuDe);
            cmd.Parameters.AddWithValue("@NoiDungDemo", noidungdemo);
            cmd.Parameters.AddWithValue("@NoiDung", noidung);
            cmd.Parameters.AddWithValue("@TenDangNhap_NV", taikhoan);
            cmd.ExecuteNonQuery();
            lblThongBao.Text = "Thêm tin tức thành công";
            conn.Close();
        }

        private void themButton_Click(object sender, EventArgs e)
        {
            themTinTuc(tenanh, txtTieuDe.Text, txtNoiDungChinh.Text, txtNoiDung.Text);
            xoaThongTin();
        }

        private void thoatButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void chonAnhButton_Click()
        {
            Config file = new Config(Application.StartupPath + "\\config.ini");
            string ketnoi = file.ReadValue("duongdanhinhanh", "hinhanh");
            OpenFileDialog f = new OpenFileDialog();
            f.InitialDirectory = ketnoi;
            f.Filter = "Images Files (*.bmp, *jpg, *.png)|*.bmp; *.jpg; *.png|All Files (*.*)|*.*";
            if (f.ShowDialog() == DialogResult.OK)
            {
                filename = f.FileName;
                tenanh = System.IO.Path.GetFileName(f.FileName);
                hinhAnhTinTuc.Image = Image.FromFile(filename);
            }
        }
    }
}
